//
//  HTSearchScreen.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/1.
//

import SwiftUI
import AppLovinSDK

struct HTSearchScreen: View {

  @Environment(\.presentationMode) var var_presentationMode: Binding<PresentationMode>
  @EnvironmentObject private var var_store: HTAppStore
  @State var var_searchText: String = ""
  @State var var_placeholder: String = ""
  @State private var var_showHistory: Bool = true
  @State private var var_showResult: Bool = false
  @State private var var_suggestions: [String] = []
  @State private var var_isEditing: Bool = false
  @State private var var_historyStrs: [String] = []
  @State private var var_type: String = ""
  @State private var var_kid: String = ""

  var body: some View {
    HTNavigationBar{
      if (var_showHistory) {
        HTSearchHistoryView() { searchText in
          var_type = "5"
          var_kid = "15"
          var_isEditing = false
          UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
          ht_search(searchText: searchText)
        }
      }
        // 搜索内容推荐结果列表
      if (var_suggestions.count > 0) {
        HTSearchListView(var_suggestions: $var_suggestions) { suggestion in
          var_isEditing = false
          var_type = "3"
          var_kid = "1"
          ht_search(searchText: suggestion)
        }
      }
        // 搜索结果页
      if (var_showResult) {
        HTSearchResultView(var_type: $var_type, var_searchText: var_searchText)
      }
    }
    .ht_backButtonTapAction(backButtonTapAction: {
      if (var_showResult) {
        var_showHistory = true
        var_showResult = false
        var_suggestions = []
        var_searchText = ""
      } else {
        self.var_presentationMode.wrappedValue.dismiss()
      }
    })
    .ht_navigationBarTitleView(titleView: {
      HTSearchBar(var_placeholder: var_placeholder, var_onChange: { searchText in
        if (searchText != "" && var_isEditing) {
          var_showHistory = false
          var_showResult = false
          var_store.dispatch(HTEnumSearchAction.ht_searchAssociationalWord(keyword: searchText, andCallback: { suggestions in
            var_suggestions = suggestions!
          }))
        }
        if (searchText == "") {
          var_showHistory = true
          var_showResult = false
          var_suggestions = []
        }
      }, var_onEditingChanged: { edit in
        var_isEditing = edit
      }, var_onSearch: { searchText in
        var_type = "1"
        var_kid = "1"
        ht_search(searchText: searchText)
      }, var_text: $var_searchText)
    })
    .ht_maxWidth(leading: 40, trailing: 15)
    .onChange(of: var_isEditing, perform: { newValue in
    })
  }

  func ht_search(searchText: String) {
    var_searchText = searchText != "" ? searchText : var_placeholder
    var_showHistory = false
    var_showResult = true
    var_suggestions = []
    if (var_searchText != "") {
      var_store.dispatch(HTEnumSearchAction.ht_addSearchHistory(name: var_searchText))
      HTNetworkManager().ht_requestForBuried(.htEnumSearchClNew(var_kid: var_kid, var_word: var_searchText, var_type: var_type))
    }
  }
}
